//
//  NSString+Format.m
//  AFNetworking
//
//  Created by abc on 2019/1/14.
//

#import "NSString+Format.h"

@implementation NSString (Format)

+ (NSString *)stringForPayerTime:(CGFloat)time {
    return [NSString stringWithFormat:@"%02ld:%02ld", (long)time/60, (long)time%60];
}

+ (NSString *)middleTruncatingStringWithString:(NSString *)string
                                  suffixString:(NSString *)suffix
                                    stringFont:(nonnull UIFont *)font
                                      maxWidth:(CGFloat)maxWidth {
    NSString *displayName = @"";
    for (int i=0 ; i<string.length; i++) {
        if (i==0) {
            displayName = [string stringByAppendingString:suffix];
        } else {
            displayName = [[[string substringToIndex:string.length - i] stringByAppendingString:@"..."] stringByAppendingString:suffix];
        }
        if ([displayName boundingRectWithSize:CGSizeMake(MAXFLOAT, 30)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName:font}
                                      context:nil].size.width < maxWidth) break;
    }
    return displayName;
}

- (NSString *)firstWord
{
    return [self componentsSeparatedByString:@" "].firstObject ?: @"";
}

- (NSString *)compatibleSubStringToIndex:(NSUInteger)index
{
    for (int i=0; i<index; i++) {
        if (self.length >= index - i) {
            return [self substringToIndex:(index - i)];
        }
    }
}

- (NSUInteger)lineNumberOfFont:(UIFont *)font size:(CGSize)size
{
    if (self.length <= 0) return 0;
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.copy
                                                                               attributes:@{NSFontAttributeName : font}];
    return  [string boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                 context:nil].size.height / font.lineHeight;
}

- (NSUInteger)lineNumberOfFontNumber:(CGFloat)fontNumber size:(CGSize)size
{
    if (self.length <= 0) return 0;
    
    UIFont *font = [UIFont systemFontOfSize:fontNumber];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.copy
                                                                               attributes:@{NSFontAttributeName : font}];
    return  [string boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                 context:nil].size.height / font.lineHeight;
}

- (NSString *)formattedAZPriceString {
    NSString *formattedString = self.copy;
    
    if (![formattedString containsString:@"."]) {
        return formattedString;
    }
    
    NSString *preString = @"";
    NSString *sufString = formattedString;
    
    if ([formattedString containsString:@"$"]) {
        NSInteger index = [formattedString rangeOfString:@"$"].location + 1;
        
        preString = [formattedString substringToIndex:index];
        sufString = [formattedString substringFromIndex:index];
    }
    
    sufString = [NSString stringWithFormat:@"%.2f", sufString.floatValue];
    
    formattedString = [preString stringByAppendingString:sufString];
    
    return formattedString;
}

@end
